import { createSlice, PayloadAction } from '@reduxjs/toolkit'
import { produce } from 'immer'

export type PageInfoType = {
  title: string
  desc?: string
  js?: string
  css?: string
  isPublished?: boolean
}

const INIT_STATE: PageInfoType = {
  title: "",
  desc: "",
  js: "",
  css: "",
};

const pageInfoSlice = createSlice({
  name: "pageInfo",
  initialState: INIT_STATE,
  reducers: {
    resetPageInfo: (
      _state: PageInfoType,
      action: PayloadAction<PageInfoType>
    ) => {
      return action.payload;
    },

    // 修改标题
    changePageTitle: produce(
      (draft: PageInfoType, action: PayloadAction<string>) => {
        draft.title = action.payload;
      }
    ),
    // 修改isPublished
    changePageIsPublished: produce(
      (draft: PageInfoType, action: PayloadAction<boolean>) => {
        draft.isPublished = action.payload;
      }
    ),
  },
});

export const { resetPageInfo, changePageTitle, changePageIsPublished } =
  pageInfoSlice.actions;

export default pageInfoSlice.reducer
